﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 29/12/2009
 * Time: 14:00
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of PurchaseDetail.
	/// </summary>
	public class PurchaseDetail : csharp.common.DefaultClass, csharp.common.IClass
	{
		#region Declaration variables
			private String _product_id; 
			private String _purchase_id;
			private String _lot; 
			private String _amount; 
			private String _price; 
			private String _percentage_discount; 
			private String _value_discount; 
			private String _cfop_id; 
			private String _icms_base; 
			private String _icms_value; 
			private String _icms_percentage; 
			private String _ipi_value; 
			private String _item_value; 
		#endregion
		
		public PurchaseDetail()
		{
			clear();
			
			gsql.table = "common.purchase_detail";
			_sequence  = "common.purchase_detail_purchase_detail_id_seq";
		}
		
		#region Declaration methods Gets end Sets
			public string Purchase_id {
				get { return _purchase_id; }
				set { _purchase_id = value; }
			}
			
			public string Product_id {
				get { return _product_id; }
				set { _product_id = value; }
			}
			
			public string Lot {
				get { return _lot; }
				set { _lot = value; }
			}
			
			public string Amount {
				get { return _amount; }
				set { _amount = value; }
			}
			
			public string Price {
				get { return _price; }
				set { _price = value; }
			}
			
			public string Percentage_discount {
				get { return _percentage_discount; }
				set { _percentage_discount = value; }
			}
			
			public string Value_discount {
				get { return _value_discount; }
				set { _value_discount = value; }
			}
			
			public string Cfop_id {
				get { return _cfop_id; }
				set { _cfop_id = value; }
			}
			
			public string Icms_base {
				get { return _icms_base; }
				set { _icms_base = value; }
			}
			
			public string Icms_value {
				get { return _icms_value; }
				set { _icms_value = value; }
			}
			
			public string Icms_percentage {
				get { return _icms_percentage; }
				set { _icms_percentage = value; }
			}
			
			public string Ipi_value {
				get { return _ipi_value; }
				set { _ipi_value = value; }
			}
			
			public string Item_value {
				get { return _item_value; }
				set { _item_value = value; }
			}	
		#endregion
		
		public void clear()
		{
			_product_id 			= String.Empty;
			_purchase_id            = String.Empty;
			_lot 					= String.Empty;
			_amount 				= String.Empty;
			_price 					= String.Empty;
			_percentage_discount 	= String.Empty;
			_value_discount 		= String.Empty;
			_cfop_id 				= String.Empty;
			_icms_base 				= String.Empty;
			_icms_value 			= String.Empty;
			_icms_percentage 		= String.Empty;
			_ipi_value 				= String.Empty;
			_item_value 			= String.Empty;
		}
		
		public override bool delete()
		{
			return base.delete();
		}
		
		public override void get(System.Data.DataRow dr)
		{
			_id 					= dr["purchase_detail_id"].ToString();
			_product_id  			= dr["purchase_detail_product_id"].ToString();
			_purchase_id            = dr["purchase_detail_purchase_id"].ToString();
			_lot  					= dr["purchase_detail_lot"].ToString();
			_amount  				= dr["purchase_detail_amount"].ToString();
			_price  				= dr["purchase_detail_price"].ToString();
			_percentage_discount  	= dr["purchase_detail_percentage_discount"].ToString();
			_value_discount  		= dr["purchase_detail_value_discount"].ToString();
			_cfop_id  				= dr["purchase_detail_cfop_id"].ToString();
			_icms_base  			= dr["purchase_detail_icms_base"].ToString();
			_icms_value  			= dr["purchase_detail_icms_value"].ToString();
			_icms_percentage  		= dr["purchase_detail_icms_percentage"].ToString();
			_ipi_value  			= dr["purchase_detail_ipi_value"].ToString();
			_item_value  			= dr["purchase_detail_item_value"].ToString();
		}
		
		public override bool save()
		{
			return base.save();
		}
		
		public override void setGsql()
		{
			gsql.setValue(_product_id  			                                   , "purchase_detail_product_id");
			gsql.setValue(_purchase_id											   , "purchase_detail_purchase_id");
			gsql.setValue(_lot  						                           , "purchase_detail_lot");
			gsql.setValue(_amount  					                               , "purchase_detail_amount");
			gsql.setValue(_price  				                                   , "purchase_detail_price");
			gsql.setValue(_percentage_discount 	                                   , "purchase_detail_percentage_discount");
			gsql.setValue(_value_discount  		                                   , "purchase_detail_value_discount");
			gsql.setValue(_cfop_id  				                               , "purchase_detail_cfop_id");
			gsql.setValue(_icms_base  				                               , "purchase_detail_icms_base");
			gsql.setValue(_icms_value  			                                   , "purchase_detail_icms_value");
			gsql.setValue(_icms_percentage  		                               , "purchase_detail_icms_percentage");
			gsql.setValue(_ipi_value  				                               , "purchase_detail_ipi_value");
			gsql.setValue(_item_value  			                                   , "purchase_detail_item_value");
		}
		
		public override bool store(csharp.common.GenerateSql.TCommandType ctype)
		{
			return base.store(ctype);
		}
		
		public override bool update()
		{
			return base.update();
		}
		
		public void changeTableToTemp()
		{
			gsql.table = "common.purchase_detail_temp";
			_sequence = "common.purchase_detail_temp_purchase_detail_temp_id_seq";
		}
		
		public void changeTableToMain()
		{
			gsql.table = "common.purchase_detail";
			_sequence = "common.purchase_detail_purchase_detail_id_seq";
		}
		
	}
	
	public class PurchaseDetails : csharp.common.DefaultList, csharp.common.IList
	{
		public PurchaseDetails()
		{
			gsql.table = "common.purchase_detail";
		}
		
		public PurchaseDetail get(Int32 index)
		{
			return (PurchaseDetail)items[index];
		}
		
		public PurchaseDetail get()
		{
			return (PurchaseDetail)items[index];
		}	
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			
			return mountList();
		}
		
		public bool fillList(String purchaseId)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "purchase_detail_purchase_id = "+purchaseId;
			return mountList();
		}
		
		public bool mountList()
		{
			Int32 tam;
			if (!gsql.execute())
				return false;
			
			items.Clear();
			tam= gsql.getRowCount();
			for (int i=0; i < tam; i++)
			{
				PurchaseDetail pd = new PurchaseDetail();
				pd.get(gsql.getRow(i));
				items.Add(pd);
			}
			return true;
		}
		
		public bool deleteByPurchaseId(String purchaseId)
		{
			gsql.prepare();
			gsql.commandType = common.GenerateSql.TCommandType.CMD_DELETE;
			gsql.conditionalWhere = "purchase_detail_purchase_id ="+ purchaseId;
			return gsql.execute();
		}
	
		public void changeTableToTemp()
		{
			gsql.table = "common.purchase_detail_temp";
		}
		
		public void changeTableToMain()
		{
			gsql.table = "common.purchase_detail";
		}		
	}
}
